//
//  HSYBaseNavigationController.m
//  高仿暴走斗图
//
//  Created by key on 16/3/16.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseNavigationController.h"
#import "UINavigationController+Manager.h"
#import "UIImage+Canvas.h"
#import "UIView+Gesture.h"
#import <math.h>
#import "HSYBaseNavigationController+Animation.h"
#import "UINavigationBar+customBar.h"
#import "HSYBaseNavigationController+UI.h"
#import "HSYBaseNavigationController+MoveAnimation.h"

@interface HSYBaseNavigationController ()<UIGestureRecognizerDelegate> {
    
    CGFloat proportion;
}

@property (nonatomic, strong) UIPanGestureRecognizer *panGestrue;
@property (nonatomic, assign) BOOL isMoving;
@property (nonatomic, assign) CGPoint touchBeganPoint;

@end

@implementation HSYBaseNavigationController

@synthesize screenViewControllers = _screenViewControllers;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    proportion = PROPORTION;
    self.navigationBarColor = [[HSYColorHelper shareInstance] navBarColor];
    
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    if (self.isCanDragBack) {
        // 禁用 iOS7 返回的pop手势
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
        [self.screenViewControllers addObject:[UIImage captureImageInView:self.view]];
    } else {
        
        // 启动 iOS7 返回的pop手势
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}


- (void)setTransitionsAnimationType:(kHSYTransitionsAnimationType)animationType withBarColor:(UIColor *)barColor {
    
    _screenViewControllers = [[NSMutableArray alloc] init];
    _animationType = animationType;
    self.isCanDragBack = YES;
}

- (void)setIsCanDragBack:(BOOL)isCanDragBack {
    
    _isCanDragBack = isCanDragBack;
    [self isCanDragViewController:isCanDragBack];
}


#pragma mark - Setting NavBar Color

- (void)setNavigationBarColor:(UIColor *)navigationBarColor {
    
    _navigationBarColor = navigationBarColor;
    [self.navigationBar clearBarBottomLineWithTintColor:navigationBarColor];
}


#pragma mark - Push And Pop

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.view.userInteractionEnabled) {
        
        NSInteger count = [self.viewControllers count];
        if (count > 0) {
            [self.screenViewControllers addObject:[UIImage captureImageInView:self.view]];
        }
        [super pushViewController:viewController animated:animated];
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    
    NSUInteger count = [self.viewControllers count];
    if (self.view.userInteractionEnabled) {
        if (count > 1) {
            [self.screenViewControllers removeLastObject];
            UIViewController *vc = [super popViewControllerAnimated:animated];
            return vc;
        }
    }
    return nil;
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    NSUInteger count = [self.viewControllers count];
    if (self.view.userInteractionEnabled) {
        if (count > 1) {
            NSInteger index = [self findNavigationControllerSubViewControllerIndexToViewController:viewController];
            for (NSInteger i = index; i < self.viewControllers.count; i++) {
                [self.screenViewControllers removeObjectAtIndex:i];
            }
            return [super popToViewController:viewController animated:animated];
        }
        
    }
    return nil;
}


- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    
    if (self.view.userInteractionEnabled) {
        [self.screenViewControllers removeAllObjects];
        return [super popToRootViewControllerAnimated:animated];
    }
    return nil;
}


#pragma mark - Set Drag Gestures

- (void)isCanDragViewController:(BOOL)canDrag {
    
    if (canDrag) {
        
        [self addPanGesture];
    } else {
        
        [self removePanGesture];
    }
}

- (void)addPanGesture {
    
    if (self.panGestrue) {
        return;
    }
    self.panGestrue = [UIView addGestureRecognizerWithAction:@selector(paningGestureReceive:) delegate:self gestureRecognizerClass:[UIPanGestureRecognizer class]];
    
    [self.panGestrue delaysTouchesBegan];
    [self.view addGestureRecognizer:self.panGestrue];
}

- (void)removePanGesture {
    
    NSParameterAssert(self.panGestrue);
    [self.view removeGestureRecognizer:self.panGestrue];
}


#pragma mark - PaningGestureReceive  

- (void)paningGestureReceive:(UIPanGestureRecognizer *)panGesture {
    
    if (self.viewControllers.count <= 1 || !self.isCanDragBack) {
        return;
    }
    
    self.isMoving = YES;
    CGPoint beganPoint = [panGesture locationInView:[UIScreen getKeyWindows]];
    [self transitionsStatusWithState:panGesture.state beganPoint:beganPoint];
    if (self.isMoving) {
        switch (self.animationType) {
                
            case kHSYTransitionsAnimationTypeMove: {
                
                [self moveViewWithX:beganPoint.x - self.touchBeganPoint.x withBlackMask:self.blackMask startBackViewX:self.startBackViewX lastScreenShotView:self.lastScreenShotView];
            }
                break;
                
            case kHSYTransitionsAnimationTypeCard: {
                
                [self cardViewWithX:beganPoint.x - self.touchBeganPoint.x withBlackMask:self.blackMask startBackViewX:self.startBackViewX lastScreenShotView:self.lastScreenShotView];
            }
                break;
                
            case kHSYTransitionsAnimationTypeRotation: {
                
            }
                break;
                
            default:
                break;
        }
    }
}


- (void)transitionsStatusWithState:(UIGestureRecognizerState)state beganPoint:(CGPoint)beganPoint {
    
    switch (state) {
        case UIGestureRecognizerStateBegan: {
            
            [self beginWithPoint:beganPoint];
        }
            break;
            
        case UIGestureRecognizerStateCancelled: {
            
            self.isMoving = NO;
            [self endStutasWithLocation:0 endAnimationType:kHSYAnimationEndTypeEnd];
        }
            break;
            
        case UIGestureRecognizerStateEnded: {
            
            [self endWithPoint:beganPoint];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - Move Type

- (void)beginWithPoint:(CGPoint)point {
    
    self.touchBeganPoint = point;
    CGRect frame = self.view.frame;
    [self addPOPInterface:frame];
    [self createScreenshotImageWithTransitionsAnimationType:self.animationType];
}

- (void)endWithPoint:(CGPoint)point {
 
    self.isMoving = NO;//取消或者结束的手指状态都要设置为NO
    CGFloat x = point.x - self.touchBeganPoint.x;
    if (x > fabs(proportion)) {
        
        [self endStutasWithLocation:IPHONE_WIDTH endAnimationType:kHSYAnimationEndTypeEnd];
    } else {
        
        [self endStutasWithLocation:0 endAnimationType:kHSYAnimationEndTypeCancel];
    }
}

- (void)setShowsShadow:(BOOL)showsShadow {
    
    _showsShadow = showsShadow;
    [self updateShadowForCenterView];
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isKindOfClass:[UIScrollView class]]) {
        return YES;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
